const express = require('express')
const adminModel = require('../db/modle/adminModel')
var md5 = require('md5')
const addAdminReg = require('../middleware/addAdminReg')
const isLoginMiddle = require('../middleware/isLoginMiddle')
const route = express.Router()
route.post('/addAdmin',  isLoginMiddle,addAdminReg, async (req, res) => {
    const {
        adminName,
        password,
        regTime
    } = req.body
    try {
        const findResult = await adminModel.findOne({
            adminName
        })
        if (findResult) {
            return res.send({
                code: 10001,
                msg: "用户名已经被注册",
                data: null
            })
        }
        await adminModel.create({
            adminName,
            password: md5(password + 66),
            regTime
        })
        res.send({
            code: 10000,
            msg: "注册成功",
            data: null
        })
    } catch (e) {
        console.log('添加管理员异常' + e.message);
        res.send({
            code: 10003,
            message: "其他类型错误",
            data: null
        })
    }
})
module.exports = route